﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.Lib;
using QuanLyBanHang.Report;

namespace QuanLyBanHang.GUI
{
    public partial class frmBanHang : Form
    {
        #region "Varialbes"
        public string _MaKH;
        public string _Hang;
        public DataTable _dtbKH = KetNoi.GetTable("select  top 0 TenSP,DonGiaBan,MaDoTuoi,ChatLieu,MauSac,NhaSanXuat from tblSanPham");

        public DataTable _dtbHoaDon = KetNoi.GetTable("select  * from tblHoaDon");
        public DataTable _dtbCTHD = KetNoi.GetTable("select * from tblChiTietHD");
        private int _TongTien = 0;
        private string _user;
        private int _ThanhTien = 0;
        private int _STT = 0;
        #endregion
        public frmBanHang()
        { }
        public frmBanHang(string username)
        {
            InitializeComponent();
            _dtbKH = KetNoi.GetTable("select  top 0 TenSP,DonGiaBan,MaDoTuoi,ChatLieu,MauSac,NhaSanXuat from tblSanPham");

            _user = username;
            lbnNhanVien.Text =username;
        }

        private void frmBanHang_Load(object sender, EventArgs e)
        {
            //lbnNhanVien.Text = 
            var col1 = new DataGridViewTextBoxColumn();
            var col2 = new DataGridViewTextBoxColumn();
            var col3 = new DataGridViewTextBoxColumn();
            var col4 = new DataGridViewTextBoxColumn();
            var col5 = new DataGridViewTextBoxColumn();
            var col6 = new DataGridViewTextBoxColumn();
            var col7 = new DataGridViewTextBoxColumn();
            var col8 = new DataGridViewTextBoxColumn();
            var col9 = new DataGridViewTextBoxColumn();

            col1.Name = "STT";
            col2.Name = "TenSP";
            col3.Name = "DonGiaBan";
            col4.Name = "SoLuong";
            col5.Name = "MaDoTuoi";
            col6.Name = "ChatLieu";
            col7.Name = "MauSac";
            col8.Name = "NhaSanXuat";
            col9.Name = "ThanhTien";


            col1.HeaderText = "STT";
            col2.HeaderText = "Tên sản phẩm";
            col3.HeaderText = "Đơn giá";
            col4.HeaderText = "Số lượng";
            col5.HeaderText = "Độ tuổi";
            col6.HeaderText = "Chất liệu";
            col7.HeaderText = "Màu sắc";
            col8.HeaderText = "Nhà sản xuất";
            col9.HeaderText = "Thành tiền";

            dataGridView1.Columns.Add(col1);
            dataGridView1.Columns.Add(col2);
            dataGridView1.Columns.Add(col3);
            dataGridView1.Columns.Add(col4);
            dataGridView1.Columns.Add(col5);
            dataGridView1.Columns.Add(col6);
            dataGridView1.Columns.Add(col7);
            dataGridView1.Columns.Add(col8);
            dataGridView1.Columns.Add(col9);
        }

        private void btnKhachHang_Click(object sender, EventArgs e)
        {
            frmDSKH frmKH = new frmDSKH();
            frmKH.ShowDialog();
            _MaKH = frmKH._MaKH;
            _dtbKH = KetNoi.GetTable("select * from tblKhachHang where MaKH = '" + _MaKH + "'");
            txtMaKH.Text = _dtbKH.Rows[0]["MaKH"].ToString();
            txtHoTen.Text = _dtbKH.Rows[0]["TenKH"].ToString();
            txtSoDT.Text = _dtbKH.Rows[0]["SoDT"].ToString();
            txtDiaChi.Text = _dtbKH.Rows[0]["DiaChi"].ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            frmDSHang frHang = new frmDSHang();
            frHang.ShowDialog();
        }
        private void btnThemSP_Click(object sender, EventArgs e)
        {
            if ((txtMaSP.Text == null) || (txtMaSP.Text == ""))
            {
                MessageBox.Show("Chưa chọn mã sản phẩm");
            }
            else
            {
                DataTable _SP = KetNoi.GetTable("select TenSP,DonGiaBan,MaDoTuoi,ChatLieu,MauSac,NhaSanXuat from tblSanPham where TenSP = '" + txtMaSP.Text + "'");

                string[] row;
                _ThanhTien = Int32.Parse(_SP.Rows[0]["DonGiaBan"].ToString()) * Int32.Parse(txtSoLuong.Text.Trim());
                _TongTien += _ThanhTien;
                lbnTongTien.Text = _TongTien.ToString();
                row = new string[] {
                    (++_STT).ToString(),
                    _SP.Rows[0]["TenSP"].ToString(),
                    _SP.Rows[0]["DonGiaBan"].ToString(),
                    txtSoLuong.Text,
                    _SP.Rows[0]["MaDoTuoi"].ToString(),
                    _SP.Rows[0]["ChatLieu"].ToString(),
                    _SP.Rows[0]["MauSac"].ToString(),
                    _SP.Rows[0]["NhaSanXuat"].ToString(),
                _ThanhTien.ToString()};
                dataGridView1.Rows.Add(row);
                txtMaSP.Text = "";
            }
        }
        private void btnTimSP_Click(object sender, EventArgs e)
        {
            frmDSHang frmHang = new frmDSHang();
            frmHang.ShowDialog();
           txtMaSP.Text = frmHang._MaHang;
           _Hang = frmHang._Hang;

        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if ((txtMaHD.Text == "") || (txtHoTen.Text == ""))
            {
                MessageBox.Show("Chưa nhập đủ thông tin");
            }
            else
            {
                if (txtMaKH.Text == "")
                {
                    MessageBox.Show("Chưa có mã khách hàng, nhập mã khách hàng để thêm mới");
                    KetNoi.ExcuteSQL("insert into tblKhachHang(MaKH,TenKH,DiaChi,SoDT) values('" + txtMaKH.Text + "','" + txtHoTen.Text + "','" + txtDiaChi.Text + "','" + txtSoDT.Text + "')");
                }
                DataRow drow = _dtbHoaDon.NewRow();
                drow["MaHD"] = txtMaHD.Text;
                drow["MaKH"] = txtMaKH.Text.Trim();
                drow["MaNhanVien"] = lbnNhanVien.Text;
                drow["NgayLap"] = dtpNgayLap.Value;
                drow["GhiChu"] = txtGhiChu.Text;
                int k = KetNoi.ExcuteSQL("insert into tblHoaDon(MaHD,MaKH,MaNhanVien,NgayLap,GhiChu) values('" + drow["MaHD"] + "','" + drow["MaKH"] + "','" + drow["MaNhanVien"] + "','" + drow["NgayLap"] + "','" + drow["GhiChu"] + "')");

                //Them Chi tiet HD
                DataRow newr = _dtbCTHD.NewRow();
                for (int a = 0; a < dataGridView1.Rows.Count; a++)
                {
                    newr["MaHD"] = txtMaHD.Text;
                    string _strTenSP = dataGridView1.Rows[a].Cells["TenSP"].Value.ToString();
                    newr["MaSP"] = KetNoi.GetTable("select * from tblSanPham where TenSP = '" + _strTenSP + "'").Rows[0]["MaSP"].ToString();
                    newr["SoLuong"] = dataGridView1.Rows[a].Cells["SoLuong"].Value;
                    //newr["DonGia"] = dataGridView1.Rows[a].Cells["DonGiaBan"].Value;
                    int m = KetNoi.ExcuteSQL("insert into tblChiTietHD(MaHD,MaSP,SoLuong) values('" + newr["MaHD"] + "','" + newr["MaSP"] + "','" + newr["SoLuong"] + "')");
                }

                if (k > 0)
                {
                    MessageBox.Show("thêm mới thành công");
                    //DataSet1 ds = new DataSet1();
                    //ds.dtbHoaDon = KetNoi.GetTable("select tblSanPham.TenSP, tblChiTietHD.SoLuong,tblSanPham.DonGiaBan "
                    //+"from tblSanPham left join tblChiTietHD on tblChiTietHD.MaSP = tblSanPham.MaSP where tblChiTietHD.MaHD = 'HDdgfd4'");
                    
                }
                else
                    MessageBox.Show("Thêm mới thất bại");
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaSP.Text = dataGridView1.Rows[e.RowIndex].Cells["MaSP"].Value.ToString();

            btnXoa.Enabled = true;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells["MaSP"].Value.ToString().Trim() == txtMaSP.Text.Trim())
                {
                    dataGridView1.Rows.RemoveAt(i);
                    dataGridView1.Refresh();
                }
            }
            lbnTongTien.Text = "0";
            int _strTOng = 0;
            for (int j = 0; j < dataGridView1.Rows.Count; j++)
            {
                _strTOng += Int32.Parse(dataGridView1.Rows[j].Cells["ThanhTien"].Value.ToString());
            }
            lbnTongTien.Text = _strTOng.ToString();
        }

        private void txtSoLuong_TextChanged(object sender, EventArgs e)
        {
            if (txtSoLuong.Text != "")
            {
                if (Int32.Parse(txtSoLuong.Text) >= Int32.Parse(lbnMax.Text))
                {
                    txtSoLuong.Text = lbnMax.Text;
                }
            }
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
            base.OnKeyPress(e);
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            crsHoaDon rpHoaDon = new crsHoaDon();
            rpHoaDon.Show();
        }
    }
}
